<?php
//include_once('addons/services/PassportService.class.php');
/**
 * 视频处理类
 * @author agui
 * @date 2012年1月5日 10：00
 * 
 */
import('Think.Services.PassportService');
class VideoAction extends Action
{
    public function _initialize() {
        $pid=Session::get('p_id');
    	if(empty($pid)){
          $this->redirect('Public/login');
        //  $this->error("请先登陆");  $pid
        }
	}
    /**
    +----------------------------------------------------------
    * 探针模式
    +----------------------------------------------------------
    */
    public function checkEnv()
    {
        load('pointer',THINK_PATH.'/Tpl/Autoindex');//载入探针函数
        $env_table = check_env();//根据当前函数获取当前环境
        echo $env_table;
    }
    //默认视频首页 
    /**
     * 视频列表
     **/
    
   public function index()
   {
    // check_session();   
     $this->display();           
     
   }
   //视频展示页面 
   public function addVideo(){
    $uid=Session::get("uid");
   // if(empty($uid)){
    //   $this->error("要重新登陆哦");
   //    $this->redirect(U("Public/login")); 
  //  }
    $list  = $this->__getVideo($uid);
    $videoslist=$list['list'];
    foreach($videoslist AS $key=>$val){
      $videoslist[$key]['time']=date('Y-m-d H:i:s',$val['ctime']); 
      $videoslist[$key]['play_url']=U('Video/playVideo/v_id/'.$val['v_id']); 
       $videoslist[$key]['title'] = $this->cut_str($val['title'],42,0,'UTF-8');
    }
    $this->assign("page",$list['page']);
    
    $this->assign("videoslist",$videoslist);
    
    $this->display();
    
   }
  //视频展示页面 
   public function playVideo(){
  //
    $v_id=$_REQUEST['v_id'];
    if($v_id<0){
        $this->error("好像该视频找不到了哦");
    }
    $list  = $this->__getVideo($uid);
    $videoslist=$list['list'];
    foreach($videoslist AS $key=>$val){
      $videoslist[$key]['time']=date('Y-m-d H:i:s',$val['ctime']); 
      $videoslist[$key]['play_url']=U('Video/playVideo/v_id/'.$val['v_id']); 
    }
     $map['v_id']=$v_id;
     $video=new VideoModel();
     $videoinfo=$video->getVideoInfo($map);
     
    $link=$videoinfo['link'];
    $type=$videoinfo['type'];
    $this->assign("videoslist",$videoslist);
    $this->assign('type',$type);
    $this->assign('link',$link);
    $this->display();
    
   }
   //处理分享的视频
 public function doAddVideo(){
    if(!isset($_SESSION['nick'])){
      $this->redirect('Public/login');
    //  $this->error("请先登陆");  
    }
  //  $this->ajaxReturn(null,'链接不可为空',0,'json');
  //  $link=$_POST['link'];
    //解析link
  // $link = 'http://v.youku.com/v_playlist/f16868142o1p0.html'; 
//  $link="http://www.tudou.com/programs/view/aPn3sHCFjmk/?FR=LIAN";
  $link=$_REQUEST['link'];
  if(empty($link)){
     $this->ajaxReturn(null,'链接不可为空',0,'json');
 }
  if(strpos($link,'youku.com')){
  $arr=explode("/",$link);
  $host = $arr[2];
  if(!empty($host)){
    $data['host']=$host;
  }
 
  $text=file_get_contents($link);
  
  $addr=strstr($text,'link2');
 // dump($addr);
  
   $l=strpos($addr,'>');
   $attr=substr($addr,14,$l-16);
   $video_swf_url=$attr;
   $data['link']=$video_swf_url;
    //获取视频标题
   preg_match("/<title>(.*)<\/title>/smUi", $text, $title);
  if (!empty($title)) { 
    $data['title'] = $title[1];
    
  }
  $data['type']=1;
  }
  elseif(strpos($link,'tudou.com')){
    
   $arr=explode("/",$link);
   $host = $arr[2];
  if(!empty($host)){
    $data['host']=$host;
  }
   $text=file_get_contents($link);
   $addr=strstr($text,'player:');
   $l=strpos($addr,',');
   $attr=substr($addr,9,$l-10);
   $video_swf_url=$attr;
   $data['link']=$video_swf_url;
    //获取视频标题
   preg_match("/<title>(.*)<\/title>/smUi", $text, $title);
    
  if (!empty($title)) { 
    $data['title'] = trim($title[1]);
   }
  $data['type']=2;
  }
   $data['uid']=Session::get('uid');
   $data['ctime'] = time();
  //写入数据库
   if($vid = D('Video', 'home')->add($data)){
    $all['type']=4;
    $all['uid']=$data['uid'];
    $all['ctime']=$data['ctime'];
    $all['tid']=$vid;
    D('summary')->add($all);
    
   $this->ajaxReturn($data,'视频添加成功！',1,'json');
   
   }
    
   $this->ajaxReturn($data,'视频添加失败！',0,'json');
    
   
  // $this->assign('link',$video_swf_url);
  // $this->display("Tpl/default/Video/playVideo.html");
  }
   /**
    *__getblog
    * 获得blog列表
    * @param int|array|string $uid uid
    * @access private
    * @return void
    */
   private function __getVideo ($uid)
    {
      //给blog对象的uid属性赋值               
      $video=new VideoModel();             
      return  $video->getVideoList($uid);
    } 
/**
 * 跳转到第一次方法
 * agui
 */

    public function toFirstIndex()
    {
        $this->assign('jumpUrl',U('First/Index'));   
    }
    
     function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
      { 
            if($code == 'UTF-8') 
            { 
            $pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
            preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)); 
            return join('', array_slice($t_string[0], $start, $sublen)); 
            } 
            else 
            { 
            $start = $start*2; 
            $sublen = $sublen*2; 
            $strlen = strlen($string); 
            $tmpstr = ''; for($i=0; $i<$strlen; $i++) 
            { 
            if($i>=$start && $i<($start+$sublen)) 
            { 
            if(ord(substr($string, $i, 1))>129) 
            { 
            $tmpstr.= substr($string, $i, 2); 
            } 
            else 
            { 
            $tmpstr.= substr($string, $i, 1); 
            } 
            } 
            if(ord(substr($string, $i, 1))>129) $i++; 
            } 
            if(strlen($tmpstr)<$strlen ) $tmpstr.= ""; 
            return $tmpstr; 
            } 
        } 
  
}
?>